﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using PPI.Client.Logic.Inspection;
using PPI.Client.Utils;
using PPI.Entities.Inspections;
using VMind.Global;
using ColumnHeader = DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class TreeGridInspection : TreeGridBase
    {
        public TreeGridInspection()
        {
            InitializeComponent();
        }

        protected List<EInspectionStructure> m_Structures;
        public List<EInspectionStructure> Structures
        {
            get { return m_Structures; }
            set { m_Structures = value; }
        }

        protected override bool ShowMenu()
        {
            return false;
            //return GetChildId(treeGrid.SelectedNode) != Int32.MinValue;
        }
        protected override void LoadTree()
        {
            treeGrid.Nodes.Clear();
            treeGrid.Columns.Clear();
            m_ChildElementStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(239)))), ((int)(((byte)(242)))));


            #region Columns

            #region BasePositionCollumns

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chVisit = new ColumnHeader("Structure");
            chVisit.Width.Absolute = 100;

            ColumnHeader chProject = new ColumnHeader("Project");
            chProject.Width.Absolute = 100;

            ColumnHeader chContact = new ColumnHeader("Contact");
            chContact.Width.Absolute = 100;
            
            ColumnHeader chVisitType = new ColumnHeader("Visit Type");
            chVisitType.Width.Absolute = 100;

            ColumnHeader chTopType = new ColumnHeader("Top Type");
            chTopType.Width.Absolute = 100;

            ColumnHeader chAsset = new ColumnHeader("Asset");
            chAsset.Width.Absolute = 100;


            #endregion

            treeGrid.Columns.Add(chVisit);
            treeGrid.Columns.Add(chAsset);

            treeGrid.Columns.Add(chLatitude);
            treeGrid.Columns.Add(chLongitude);
            treeGrid.Columns.Add(chAltitude);
            treeGrid.Columns.Add(chSatelliteCount);
            treeGrid.Columns.Add(chSatelliteTime);
            treeGrid.Columns.Add(chDate);

            treeGrid.Columns.Add(chProject);
            treeGrid.Columns.Add(chContact);
            treeGrid.Columns.Add(chVisitType);
            treeGrid.Columns.Add(chTopType);

            bool hasNotAssignedInspecion = m_Structures.FindIndex(
                delegate(EInspectionStructure s)
                {
                    return s.Id == Int32.MinValue;
                }) > -1;

            if (!hasNotAssignedInspecion)
            {
                Node visitNode = CreateStructureNode(new EInspectionStructure(Int32.MinValue));
                treeGrid.Nodes.Add(visitNode);
            }
            foreach (EInspectionStructure visit in m_Structures)
            {
                Node visitNode = CreateStructureNode(visit);
                treeGrid.Nodes.Add(visitNode);
            }
        }
        protected override void ShowMenuOptions()
        {
            base.ShowMenuOptions();
            cmiDelete.Enabled = true;
            cmiEditWhithOutImages.Enabled = false;

            if (GetChildId(treeGrid.SelectedNode) == Int32.MinValue)
                cmiEdit.Visible = false;
            else
                cmiEdit.Visible = true;

            if(GetParentId(treeGrid.SelectedNode) == Int32.MinValue)
                cmiCreateStructure.Visible = true;
            else
                cmiCreateStructure.Visible = false;
        }
        protected override bool ActivateDoubleClick()
        {
            return (GetChildId(treeGrid.SelectedNode) != Int32.MinValue);
        }
        protected override bool CancelActionChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {
            if(newParentNode.Level > 0 || currentNode.Level == 0)
                return true;
            else
            {
                if (currentNode.Level > 0 && newParentNode.Level == 0 && oldParentNode.Level == 0)
                    return false;
                else
                    return true;
            }
        }
        protected override void BeforeNodeChangeParent(Node currentNode, Node oldParentNode, Node newParentNode)
        {
            EInspection tmpInspection = (EInspection)currentNode.DataKey;
            EInspectionStructure tmpInspectionStructure = (EInspectionStructure) newParentNode.DataKey;
            LInspection.SetVisit(tmpInspection, tmpInspectionStructure.Id);
            LInspection.SetStructureMobile(tmpInspection, tmpInspectionStructure, m_MobileBase.ConnectionString);

        }

        private Node CreateStructureNode(EInspectionStructure structure)
        {
            Node visitNode = new Node();
            visitNode.DataKey = structure;
            visitNode.Text = structure.Id.ToString();
            if (structure.Id == Int32.MinValue)
                visitNode.Text = "Not Assigned";
            if (structure.UtilityAsset.Id != Int32.MinValue)
                visitNode.Cells.Add(new Cell(structure.UtilityAsset.Id.ToString()));
            else
                visitNode.Cells.Add(new Cell(""));

            if (structure.Latitude != double.MinValue)
                visitNode.Cells.Add(new Cell(structure.Latitude.ToString()));
            else
                visitNode.Cells.Add(new Cell(""));

            if (structure.Longitude != double.MinValue)
                visitNode.Cells.Add(new Cell(structure.Longitude.ToString()));
            else
                visitNode.Cells.Add(new Cell(""));

            if (structure.Altitude != double.MinValue)
                visitNode.Cells.Add(new Cell(structure.Altitude.ToString()));
            else
                visitNode.Cells.Add(new Cell(""));

            if (structure.SatelliteCount > 0)
                visitNode.Cells.Add(new Cell(structure.SatelliteCount.ToString()));
            else
                visitNode.Cells.Add(new Cell("0"));

            if (structure.SatelliteTime != DateTime.MinValue)
                visitNode.Cells.Add(new Cell(structure.SatelliteTime.ToLongDateString()));
            else
                visitNode.Cells.Add(new Cell(""));
            
            if (structure.Date != DateTime.MinValue)
                visitNode.Cells.Add(new Cell(structure.Date.ToLongDateString()));
            else
                visitNode.Cells.Add(new Cell(""));

            visitNode.Cells.Add(new Cell(structure.Project.Name));
            visitNode.Cells.Add(new Cell(structure.Contact.Name));
            visitNode.Cells.Add(new Cell(structure.VisitType.Name));
            visitNode.Cells.Add(new Cell(structure.Toptype.Name));


            #region ChildCollumns

            #region BasePositionCollumns

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatelliteCount = new ColumnHeader("Satellites");
            chSatelliteCount.Width.Absolute = 100;

            ColumnHeader chSatelliteTime = new ColumnHeader("Time");
            chSatelliteTime.Width.Absolute = 100;

            ColumnHeader chDate = new ColumnHeader("Date");
            chDate.Width.Absolute = 100;



            #endregion

            ColumnHeader chInspectionId = new ColumnHeader("ID");
            chInspectionId.Width.Absolute = 100;

            ColumnHeader chYear = new ColumnHeader("Year");
            chYear.Width.Absolute = 100;

            ColumnHeader chLastVisit = new ColumnHeader("Last Visit");
            chLastVisit.Width.Absolute = 100;

            ColumnHeader chNumber = new ColumnHeader("Number");
            chNumber.Width.Absolute = 100;

            ColumnHeader chAcquisitionDate = new ColumnHeader("Acquisition Date");
            chAcquisitionDate.Width.Absolute = 100;

            ColumnHeader chTreatment = new ColumnHeader("Treatment");
            chTreatment.Width.Absolute = 100;

            ColumnHeader chClass = new ColumnHeader("Class");
            chClass.Width.Absolute = 100;

            ColumnHeader chSpecie = new ColumnHeader("Specie");
            chSpecie.Width.Absolute = 100;

            ColumnHeader chTopType = new ColumnHeader("Top Type");
            chTopType.Width.Absolute = 100;

            ColumnHeader chLength = new ColumnHeader("Length");
            chLength.Width.Absolute = 100;

            ColumnHeader lastCompany = new ColumnHeader("L. Insp");
            lastCompany.Width.Absolute = 100;

            ColumnHeader chCompany = new ColumnHeader("Company");
            chCompany.Width.Absolute = 100;

            ColumnHeader chInspector = new ColumnHeader("Inspector");
            chInspector.Width.Absolute = 100;

            ColumnHeader chMap = new ColumnHeader("Map");
            chMap.Width.Absolute = 100;

            ColumnHeader chSection = new ColumnHeader("Section");
            chSection.Width.Absolute = 100;

            ColumnHeader chKv = new ColumnHeader("KV");
            chKv.Width.Absolute = 100;

            ColumnHeader chProject = new ColumnHeader("Project");
            chProject.Width.Absolute = 100;

            ColumnHeader chContact = new ColumnHeader("Contact");
            chContact.Width.Absolute = 100;

            ColumnHeader chResultType = new ColumnHeader("Result Type");
            chResultType.Width.Absolute = 100;

            ColumnHeader chElectricalProblem = new ColumnHeader("Electrical Problem");
            chElectricalProblem.Width.Absolute = 100;

            ColumnHeader chInspectionType = new ColumnHeader("Inspection Type");
            chInspectionType.Width.Absolute = 100;

            ColumnHeader chNotes = new ColumnHeader("Notes");
            chNotes.Width.Absolute = 300;

            visitNode.NodesColumns.Add(chInspectionId);
            visitNode.NodesColumns.Add(chMap);
            visitNode.NodesColumns.Add(chSection);
            visitNode.NodesColumns.Add(chKv);
            visitNode.NodesColumns.Add(chLatitude);
            visitNode.NodesColumns.Add(chLongitude);
            visitNode.NodesColumns.Add(chAltitude);
            visitNode.NodesColumns.Add(chSatelliteCount);
            visitNode.NodesColumns.Add(chSatelliteTime);
            visitNode.NodesColumns.Add(chDate);

            visitNode.NodesColumns.Add(chYear);
            visitNode.NodesColumns.Add(chLastVisit);
            visitNode.NodesColumns.Add(chNumber);
            visitNode.NodesColumns.Add(chAcquisitionDate);
            visitNode.NodesColumns.Add(chTreatment);
            visitNode.NodesColumns.Add(chClass);
            visitNode.NodesColumns.Add(chSpecie);
            visitNode.NodesColumns.Add(chTopType);
            visitNode.NodesColumns.Add(chLength);
            visitNode.NodesColumns.Add(lastCompany);
            visitNode.NodesColumns.Add(chCompany);
            visitNode.NodesColumns.Add(chInspector);
            visitNode.NodesColumns.Add(chProject);
            visitNode.NodesColumns.Add(chContact);
            visitNode.NodesColumns.Add(chResultType);
            visitNode.NodesColumns.Add(chElectricalProblem);
            visitNode.NodesColumns.Add(chInspectionType);
            visitNode.NodesColumns.Add(chNotes);

            #endregion

            foreach (EInspection inspection in structure.Inspections)
            {
                Node poleNode = new Node();
                poleNode.Style = m_ChildElementStyle;
                poleNode.Text = inspection.Id.ToString();
                poleNode.DataKey = inspection;

                poleNode.Cells.Add(new Cell(inspection.Map.Name));
                poleNode.Cells.Add(new Cell(inspection.Section.Name));

                if (inspection.VendorVisitType != null &&inspection.VendorVisitType.Vendor_Visit_TypeId != Int32.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.VendorVisitType.Name));
                else
                    poleNode.Cells.Add(new Cell(string.Empty));

                if (inspection.Latitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Latitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Longitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Longitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Altitude != double.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Altitude.ToString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.SatelliteCount > 0)
                    poleNode.Cells.Add(new Cell(inspection.SatelliteCount.ToString()));
                else
                    poleNode.Cells.Add(new Cell("0"));
                if (inspection.SatelliteTime != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.SatelliteTime.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Date != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Date.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                if (inspection.Year != Int32.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.Year.ToString()));
                else
                    poleNode.Cells.Add(new Cell("-"));

                if (inspection.LastVisit != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.LastVisit.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                poleNode.Cells.Add(new Cell(inspection.Number));

                if (inspection.AcquisitionDate != DateTime.MinValue)
                    poleNode.Cells.Add(new Cell(inspection.AcquisitionDate.ToLongDateString()));
                else
                    poleNode.Cells.Add(new Cell(""));

                poleNode.Cells.Add(new Cell(inspection.Treatment.Name));
                poleNode.Cells.Add(new Cell(inspection.Class.Name));
                poleNode.Cells.Add(new Cell(inspection.Specie.Name));
                poleNode.Cells.Add(new Cell(inspection.TopType.Name));
                if (inspection.Length > 0)
                    poleNode.Cells.Add(new Cell(inspection.Length.ToString()));
                else
                    poleNode.Cells.Add(new Cell("-"));
                poleNode.Cells.Add(new Cell(inspection.LastCompanyName));
                poleNode.Cells.Add(new Cell(inspection.Company.Name));
                poleNode.Cells.Add(new Cell(inspection.Inspector.Name));
                poleNode.Cells.Add(new Cell(inspection.Project.Name));
                poleNode.Cells.Add(new Cell(inspection.Contact.Name));
                poleNode.Cells.Add(new Cell(inspection.Result.Resultype.Name));
                poleNode.Cells.Add(new Cell(inspection.Result.IsElectrical.ToString()));
                poleNode.Cells.Add(new Cell(inspection.Result.InspectionType.Name));
                poleNode.Cells.Add(new Cell(inspection.Result.GetNotes()));
                visitNode.Nodes.Add(poleNode);
            }
            return visitNode;
        }

        public void LoadStructures(string connectionKey)
        {
            m_Structures = LInspectionStructure.LoadProjectStructures(connectionKey);
            LoadTree();
        }

        public void RefreshStructures(List<EInspectionStructure> structures)
        {
            m_Structures = structures;
            LoadTree();
        }
        protected override int GetParentId(Node node)
        {
            EInspectionStructure tmpStructure;
            if (node.Parent != null)
                tmpStructure = (EInspectionStructure)(node.Parent.DataKey);
            else
                tmpStructure = (EInspectionStructure)node.DataKey;
            return tmpStructure.Id;
        }

        protected override int GetChildId(Node node)
        {
            Int32 returnValue = Int32.MinValue;
            try
            {
                if (node.Level > 0)
                {
                    returnValue = ((EInspection)node.DataKey).Id;
                }
            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }
            return returnValue;
        }

        public event EventHandler<TreeGridBaseEventArgs> ClickedCreateStructure;
        private void OnClickedCreateStructure(object o, TreeGridBaseEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedCreateStructure != null)
            {
                ClickedCreateStructure(o, e);
            }
        }

        void cmiCreateStructure_Click(object sender, System.EventArgs e)
        {
            OnClickedCreateStructure(this, new TreeGridBaseEventArgs(GetParentId(treeGrid.SelectedNode), GetChildId(treeGrid.SelectedNode), treeGrid.SelectedNode));
        }

        

    }
}
